package lab_16.com.acme.bankapp.domain.bank;

import java.io.Serializable;

public abstract class AbstractAccount implements Account, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5162282059552918464L;
	protected double balance;
	private int id;
	private static int id_counter = 0;

	public AbstractAccount(double balance) {
		this.balance = balance;
		this.id = gen_id();
	}

	protected static int gen_id() {
		return id_counter++;
	}

	public int getId() {
		return id;
	}

	@Override
	public long decimalValue() {
		return Math.round(balance);
	}

	@Override
	public int hashCode() {
		return id;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		return id == other.id;
	}

	@Override
	public void deposit(double amount) throws IllegalArgumentException {
		if (amount > 0) {
			balance += amount;
		} else {
			throw new IllegalArgumentException(
					"Negative amounts are not allowed.");
		}
	}

	@Override
	public double getBalance() {
		return balance;
	}

}
